    var orgid;
    $(document).ready(function() {
        $('#licensePlate').val('');
        $('#carType').val('');
        $('#reset').click();
        orgid = JSON.parse(localStorage.getItem('isLogin')).orgid;
        Judgeidentity();
        loadTableData(orgid);
        reloadTree();
        layui.use('laydate', function() {
            var laydate = layui.laydate;

            //执行一个laydate实例
            laydate.render({
                elem: '#appearanceTime' //指定元素
            });
        });
        layui.use('form', function() {
            var form = layui.form;
            form.render('select');
            //监听提交
            form.on('submit(formDemo)', function(data) {
                let formPrarm = data.field;

                let params = new FormData();

                params.append('licensePlate', data.field.licensePlate);
                params.append('personInCharge', data.field.personInCharge);
                params.append('carType', data.field.carType);
                params.append('personInChargeTel', data.field.personInChargeTel);
                params.append('model', data.field.model);
                params.append('driver', data.field.driver);
                params.append('engineNumber', data.field.engineNumber);
                params.append('driverTel', data.field.driverTel);
                params.append('frameNumber', '');
                params.append('appearanceTime', data.field.appearanceTime);
                if ($('#imagefil')[0].files[0]) {
                    params.append('file', $('#imagefil')[0].files[0]);
                } else {

                }

                //params.append('file', $('#imagefil')[0].files[0] ? $('#imagefil')[0].files[0] : new FileReader());
                // console.log(typeof($('#imagefil')[0].files[0]))
                // params.file = $('#imagefil')[0].files[0];
                if ($(this).html() == '新增车辆') {
                    addCar(params)
                } else {
                    editCar(params)
                }

                return false;
            });
        });
    })

    $('.menutab').click(function() {
        $('.menutab-con').animate({ height: 'toggle' }, 350);
    })

    //加载表格数据

    function loadTableData(orgid, searchParams) {
        if (searchParams) {
            var licensePlate = searchParams.licensePlate;
            var carType = searchParams.carType;
        } else {
            var licensePlate = '';
            var carType = '';

        }
        layui.use('table', function() {
            var table = layui.table;

            //第一个实例
            table.render({
                title: '',
                text: {
                    none: '暂无数据'
                },
                elem: '#body-content-table',
                skin: 'nob',
                url: ajaxPrefix() + '/supply/list',
                where: {
                    pageSize: 10,
                    orgid: orgid,
                    licensePlate: licensePlate,
                    carType: carType,

                },
                parseData: function(res) { //res 即为原始返回的数据
                    return {
                        "code": res.status == 200 ? 0 : 1, //解析接口状态
                        "msg": res.message, //解析提示文本
                        "count": res.result.total, //解析数据长度
                        "data": res.result.data //解析数据列表
                    };
                },
                request: {
                    pageName: 'pageNum' //页码的参数名称，默认：page
                        ,
                    limitName: 'pageSize' //每页数据量的参数名，默认：limit
                },
                page: { limit: 10, } //开启分页
                ,
                cols: [
                    [ //表头
                        {
                            title: '',
                            align: 'center',
                            templet: function(d) {
                                return `<img onclick='show_img(this)' src="/images/${d.filePath}" style="width:60px;height:100%;">`
                            },
                        },
                        { field: 'licensePlate', title: '车牌', align: 'center' },
                        { field: 'carType', title: '类型', align: 'center', },
                        { field: 'model', title: '厂牌型号', align: 'center' },
                        //{ field: 'engineNumber', title: '发动机编号', align: 'center' },
                        {
                            field: 'appearanceTime',
                            title: '出厂时间',
                            align: 'center',
                            templet: function(d) {
                                return d.appearanceTime.substr(0,10)
                            },
                        },
                        //{ field: 'personInCharge', title: '负责人', align: 'center' },
                        //{ field: 'personInChargeTel', title: '负责人电话', align: 'center' },
                        { field: 'driver', title: '驾驶员', align: 'center' },
                        { field: 'driverTel', title: '驾驶员电话', align: 'center' },
                        {
                            title: '详情',
                            align: 'center',
                            width: 120,
                            templet: function(d) {
                                let res = JSON.stringify(d);
                                return `<div><div class="table-tool">
                                    <div onclick='editCarMsg(${res})'><img src="../static/img/edit.png">编辑</div>
                                    <div onclick='deleteCar(${res})'  style="width:20px"><img src="../static/img/delete.png" style="margin-right: 0;"></div>
                                  </div></div>`
                            },
                        },
                    ]
                ]
            });

        });
    }
    //显示大图片
    function show_img(t) {
        //页面层
        layer.open({
            type: 1,
            skin: 'layui-layer-rim', //加上边框
            area: ['80%', '80%'], //宽高
            shadeClose: true, //开启遮罩关闭
            end: function(index, layero) {
                return false;
            },
            content: '<div style="text-align:center;width: 100%;height: 100%;"><img style="max-width: 100%;max-height: 99%;" src="' + $(t).attr('src') + '" /></div>'
        });
    }

    ///树
    var zTree;
    var formdata = {};
    var flag; //判断新增还是修改

    var nodeadmid; //设置全局组织id

    var selorgid;

    var west;
    var north;

    var setting = {
        async: {
            enable: true,
            type: "post",
            autoParam: ['id', 'type', 'pId', 'areaId'],
            url: getAsyncUrl,
            dataFilter: ajaxDataFilter
        },
        view: {
            expandSpeed: "fast",
            fontCss: getFont,
            showLine: true,
            dblClickExpand: false,
            showIcon: false
        },
        callback: {
            onAsyncSuccess: zTreeOnAsyncSuccess,
            onRightClick: OnRightClick,
            onClick: zTreeOnClick,
            beforeExpand: beforeExpand,
            onExpand: onExpand
        },
        data: {
            key: {
                title: "name"
            }
        }
    };


    //加载树
    function reloadTree() {
        zTree = $.fn.zTree.init($("#treeDemo"), setting);
        rMenu = $("#rMenu");
    }

    function getAsyncUrl(treeId, treeNode) {
        var orgid = JSON.parse(localStorage.getItem('isLogin')).orgid;
        var url = "/rest/organ/tree_node";
        if (!treeNode) {
            url = url + "?id=" + orgid + "&nodeType=1";
        } else {
            url = url + "?id=" + treeNode.id + "&nodeType=2";
        }

        return url;
    }
    //异步数据过滤
    function ajaxDataFilter(treeId, parentNode, childNodes) {

        return childNodes;
    }

    //异步加载成功
    function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
        if (!treeNode) { //根节点
            var rootNode = zTree.getNodesByParam("level", 0)[0];
            zTree.expandNode(rootNode, true);
            return;
        }

        var selTreeId = _selTreeId;
        if (!!selTreeId) {
            var selNode = zTree.getNodeByParam("id", selTreeId);
            if (!!selNode) {
                _selTreeId = null;
                zTree.selectNode(selNode);
                zTreeOnClick();
            }
        }
    }
    //获取选中的节点
    function getSelectedNode() {
        var treeNode = zTree.getSelectedNodes();
        if (treeNode.length == 0) {

            return false;
        }
        return treeNode[0];
    }

    //刷新子节点并确定要选中节点ID
    var _selTreeId = null;

    function reloadNode(refreshId, isParent, selTreeId) {
        var node = zTree.getNodeByParam("id", refreshId);
        if (!!node && !!isParent) {
            node = node.getParentNode();
            if (!node) {
                _selTreeId = selTreeId;
                reloadTree();
                return;
            }
        }
        if (!!node) {
            _selTreeId = selTreeId;
            zTree.reAsyncChildNodes(node, "refresh");
        }
    }



    function getFont(treeId, node) {
        return node.font ? node.font : {};
    }

    //右击事件
    function OnRightClick(event, treeId, treeNode) {

    }

    function showRMenu(type, x, y) {
        $("#rMenu ul").show();
        if (type == "root") {
            $("#m_del").hide();
            $("#m_add").hide();
        } else {
            $("#m_del").show();
            $("#m_add").show();
        }
        rMenu.css({ "top": y + "px", "left": x + "px", "visibility": "visible" });

        $("body").bind("mousedown", onBodyMouseDown);
    }

    function hideRMenu() {
        if (rMenu) rMenu.css({ "visibility": "hidden" });
        $("body").unbind("mousedown", onBodyMouseDown);
    }

    function onBodyMouseDown(event) {
        if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {
            rMenu.css({ "visibility": "hidden" });
        }
    }
    //仅能打开一个子节点
    var curExpandNode = null;

    function beforeExpand(treeId, treeNode) {
        var pNode = curExpandNode ? curExpandNode.getParentNode() : null;
        var treeNodeP = treeNode.parentTId ? treeNode.getParentNode() : null;
        for (var i = 0, l = !treeNodeP ? 0 : treeNodeP.children.length; i < l; i++) {
            if (treeNode !== treeNodeP.children[i]) {
                zTree.expandNode(treeNodeP.children[i], false);
            }
        }
        while (pNode) {
            if (pNode === treeNode) {
                break;
            }
            pNode = pNode.getParentNode();
        }
        if (!pNode) {
            singlePath(treeNode);
        }

    }

    function singlePath(newNode) {
        if (newNode === curExpandNode) return;
        if (curExpandNode && curExpandNode.open == true) {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            if (newNode.parentTId === curExpandNode.parentTId) {
                zTree.expandNode(curExpandNode, false);
            } else {
                var newParents = [];
                while (newNode) {
                    newNode = newNode.getParentNode();
                    if (newNode === curExpandNode) {
                        newParents = null;
                        break;
                    } else if (newNode) {
                        newParents.push(newNode);
                    }
                }
                if (newParents != null) {
                    var oldNode = curExpandNode;
                    var oldParents = [];
                    while (oldNode) {
                        oldNode = oldNode.getParentNode();
                        if (oldNode) {
                            oldParents.push(oldNode);
                        }
                    }
                    if (newParents.length > 0) {
                        zTree.expandNode(oldParents[Math.abs(oldParents.length - newParents.length) - 1], false);
                    } else {
                        zTree.expandNode(oldParents[oldParents.length - 1], false);
                    }
                }
            }
        }
        curExpandNode = newNode;
    }

    function onExpand(event, treeId, treeNode) {
        curExpandNode = treeNode;
    }


    //单击树节点事件
    function zTreeOnClick(event, treeId, treeNode) {
        orgid = treeNode.id
        loadTableData(orgid);

    }

    /*关闭新增弹窗*/
    $('#editcar-close').click(function() {
        $('.editcar').fadeOut(function() {
            $('#reset').click();
            $('#upload-image').show();
            $('#image-callback').hide();
            $("#image-callback").attr('src', '');
        });
    })

    $('.upload-image').click(function() {
        $('.file-input').click();
    })

    function changename(files) {
        $('.upload-image').attr('value', $('.file-input').val());
        let file = files.files[0];
        if (file) {
            var reader = new FileReader();
            reader.onload = function(evt) {
                $('#upload-image').hide();
                $('#image-callback').show();
                $("#image-callback").attr('src', evt.target.result);
            }
            reader.readAsDataURL(file);
        } else {
            $(".upload-image").html('图片上传失败');
        }
    }

    function addCar(params) {
        params.append('orgId', orgid);
        $.ajax({
            url: '/rest/v2/supply/add',
            type: 'post',
            data: params,
            dataType: "json",
            cache: false,
            processData: false,
            contentType: false,
            success: function(res) {
                if (res.status == 200) {
                    layer.msg('操作成功', { time: 1000 });
                    $("#editcar-close").click();
                    loadTableData(orgid);
                } else {
                    layer.msg('操作失败！', { time: 1000 })
                }

            }
        });

    }

    function deleteCar(res) {
        layer.confirm('确定要删除吗？', function() {
            $.ajax({
                url: '/rest/v2/supply/delete?id=' + res.id,
                type: "get",
                contentType: "application/json",
                success: function(result) {
                    if (result.message == 'OK') {
                        layer.msg('删除成功', { icon: 1, time: 1000 })
                    } else {
                        layer.msg('删除失败！', { icon: 2, time: 1000 })
                    }
                    loadTableData();
                },
            })
        });


    }
    /*新增弹窗*/
    $('#addCar').click(function() {
        $('.editcar-header span').html('新增');
        $('#formDemoSubmit').html('新增车辆');
        $('.editcar').fadeIn();
    })
    /*查询车辆信息*/
    $('#searchCar').click(function() {
        var searchParams = {};
        searchParams.licensePlate = $('#licensePlate').val();
        searchParams.carType = $('#carType').val();
        loadTableData(orgid, searchParams);

    })
    var carId;
    /*编辑车辆信息*/
    function editCarMsg(res) {
        carId = res.id
        $('.editcar-header span').html('修改');
        $('#formDemoSubmit').html('立即修改');
        $('.editcar').fadeIn();
        $('input[name=licensePlate]').val(res.licensePlate);
        $('input[name=personInCharge]').val(res.personInCharge)
        $('input[name=carType]').val(res.carType)
        $('input[name=personInChargeTel]').val(res.personInChargeTel)
        $('input[name=model]').val(res.model)
        $('input[name=driver]').val(res.driver)
        $('input[name=engineNumber]').val(res.engineNumber)
        $('input[name=driverTel]').val(res.driverTel)
        $('input[name=appearanceTime]').val(res.appearanceTime)
        $('#upload-image').hide();
        $('#image-callback').show();
        $("#image-callback").attr('src', '/images/' + res.filePath);
    }

    /*编辑*/

    function editCar(params) {
        params.append('id', carId);
        $.ajax({
            url: '/rest/v2/supply/update',
            type: 'post',
            data: params,
            dataType: "json",
            cache: false,
            processData: false,
            contentType: false,
            success: function(res) {
                if (res.status == 200) {
                    layer.msg('修改成功', { time: 1000 });
                    loadTableData(orgid);
                } else {
                    layer.msg('操作失败！', { time: 1000 })
                }

            }
        });

    }